package config;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MyBatisConfig {

    //创建pagehelper翻页插件拦截器bean
    @Bean
    public PageInterceptor pageInterceptor() {
        return new PageInterceptor();
    }

    //创建sqlsession配置信息bean
    @Bean
    public Configuration configuration() {
        Configuration configuration = new Configuration();
        configuration.setLogImpl(StdOutImpl.class);//查看Configuration类的别名找到StdOutImpl.class
        configuration.setCacheEnabled(true);
        return configuration;
    }

    //创建sqlsession对象，自动生成mapper接口代理对象
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource, PageInterceptor pageInterceptor, Configuration configuration) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setTypeAliasesPackage("pojo");
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setPlugins(pageInterceptor);
        sqlSessionFactoryBean.setConfiguration(configuration);
        return sqlSessionFactoryBean;
    }

    //创建mapper扫描映射目录bean
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("mapper");
        return mapperScannerConfigurer;
    }


}
